package pull;

import helper.ImageData;

import javax.media.jai.JAI;

import framework.filters.AbstractActiveFilter;
import framework.pipes.PullPipe;
import framework.pipes.PushPipe;

/**
 * Saves the image to a given path
 * 
 * @author Wetzel & Rusch
 * 
 */
public class ImageSaver extends AbstractActiveFilter<ImageData, ImageData> {

	public ImageSaver(PullPipe<ImageData> pullPipe, PushPipe<ImageData> pushPipe) {
		super(pullPipe, pushPipe);
	}

	String m_image;
	String m_fileType;

	public void setImage(String file) {
		m_image = file;
	}

	public void setFileType(String fileType) {
		m_fileType = fileType;
	}

	private void SaveImageToDisk() {
		JAI.create("filestore", m_inputData.getRoiImage(), m_image, m_fileType);
	}

	@Override
	public ImageData pull() {
		m_inputData = m_pullPipe.pull();
		doIt();
		return null;
	}

	@Override
	public void push(ImageData data) {
		m_inputData = data;
		doIt();
	}

	@Override
	public void startThread() {
		Runnable activeFilter = this;
		new Thread(activeFilter).start();
	}

	@Override
	public void doIt() {
		SaveImageToDisk();
	}

	@Override
	public void run() {
		m_inputData = pull();
		while (m_inputData != null) {
			m_inputData = pull();
		}
	}

}
